
import { Button } from 'antd';
import React, { useState,useReducer } from 'react'

const initState = {
    count:5
}

function reducer(state,action){
    switch(action.type){
        case "inc":return {count:state.count+1};
        case "dec":return {count:state.count-1};
        default:throw new TypeError("亲，您给的action的type不存在");
    }
}


export default function Index() {

    const [state,dispatch] = useReducer(reducer,initState);

    function addCount(){
        dispatch({
            type:"inc"
        })
    }

    function decCount(){
        dispatch({
            type:"dec"
        })
    }

  return (
    <>
    <div>useReducer</div>
    <p>count:{state.count}</p>
    <Button type="primary" onClick={addCount}>count做加法</Button>
    <Button type="primary" onClick={decCount}>count做减法</Button>
    </>
  )
}

